import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/http/utils";
import { removeEmptyValues } from "@/utils/common";
import { ResultCommon, ResultPagination, serviceType } from "@/utils/commonType";



/** 获取应用管理列表 */
export const getApiList = async (tenantId: string, params?) => {

  const result = await http.request<ResultPagination<serviceType>>(
    "get",
    baseUrlApi("/service/list/" + tenantId),
    {
      params: removeEmptyValues(params)
    }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

/** 获取服务列表 */
export const getApiAllList = async (params?) => {

  const result = await http.request<ResultPagination<serviceType>>(
    "get",
    baseUrlApi("/service/allList"),
    {
      params: removeEmptyValues(params)
    }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const saveServiceInfo = async (serviceInfo: serviceType) => {
  let result: ResultCommon<number>;
  if (serviceInfo.pid === "") {
    result = await http.request<ResultCommon<number>>(
      "post",
      baseUrlApi("/service/add"),
      {
        data: removeEmptyValues(serviceInfo)
      }
    );
  } else {
    console.log("update:" + serviceInfo.pid);
    result = await http.request<ResultCommon<number>>(
      "put",
      baseUrlApi("/service/update/" + serviceInfo.pid),
      {
        data: removeEmptyValues(serviceInfo)
      }
    );
  }
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
}

export const changeApiStatus = async (apiId: string, status: boolean) => {
  let result: ResultCommon<number>;
  if (status) {
    result = await http.request<ResultCommon<number>>(
      "put",
      baseUrlApi("/service/online/" + apiId));
  } else {
    result = await http.request<ResultCommon<number>>(
      "put",
      baseUrlApi("/service/offline/" + apiId));
  }
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
}

export const removeApi = async (apiId: string) => {
  const reslut = await http.request<ResultCommon<number>>(
    "delete",
    baseUrlApi("/service/remove/" + apiId));
  if (reslut.code != 200) {
    throw new Error(reslut.message);
  }
  return reslut;
}